package com.work.straighttalk.presenter;

import com.work.straighttalk.model.SMSMImpl;
import com.work.straighttalk.model.i.OnSMSFinishedListener;
import com.work.straighttalk.model.i.SMSMI;
import com.work.straighttalk.presenter.i.SMSPI;
import com.work.straighttalk.ui.i.SMSVI;

import cn.bmob.v3.exception.BmobException;

/**
 * Created by 肖江宏 on 16/3/3.
 */
public class SMSPImpl implements SMSPI, OnSMSFinishedListener {
    private SMSVI smsvi;
    private SMSMI smsmi;

    public SMSPImpl(SMSVI smsvi) {
        this.smsvi = smsvi;
        this.smsmi = new SMSMImpl();
    }

    /**
     * 验证手机号/用户名
     *
     * @param username
     */
    @Override
    public void validateSMS(String username) {
        if (smsvi != null) {
            smsvi.showProgress();
        }
        smsmi.login(username, this);

    }

    /**
     * 验证账号密码
     *
     * @param username
     * @param password
     */
    @Override
    public void validateSMS(String username, String password) {
        if (smsvi != null) {
            smsmi.login(username, password, this);

        }
    }

    /**
     * 让v层接口为空
     */
    @Override
    public void onDestroy() {
        smsvi = null;
    }

    /**
     * 当账号错误
     */
    @Override
    public void onUsernameError() {
        if (smsvi != null) {
            smsvi.phoneNumError();
            smsvi.hideProgress();
        }
    }

    /**
     * 当账号为空
     */
    @Override
    public void onUsernameNull() {
        if (smsvi != null) {
            smsvi.phoneNumNull();
            smsvi.hideProgress();
        }
    }


    /**
     * 当密码为空
     */
    @Override
    public void onPasswordNull() {
        if (smsvi != null) {
            smsvi.passwordNumNull();
            smsvi.hideProgress();
        }
    }

    /**
     * 当账号/手机号成功
     */
    @Override
    public void onUsernameSuccess() {
        if (smsvi != null) {
            smsvi.codeIsOK();
            smsvi.showBtnCountdown();
        }
    }

    /**
     * 成功之后跳转
     */
    @Override
    public void onSuccess() {
        if (smsvi != null) {
            smsvi.toMainActivity();
        }
    }

    /**
     * 错误回调
     *
     * @param e
     */
    @Override
    public void onError(BmobException e) {
        if (smsvi != null) {
            smsvi.showErrorToast(e);
            smsvi.hideProgress();
        }
    }
}
